/** @file    ServerTcpQt.cpp
 *  @author  Alessandro Polo
 *  @version $Id: ServerTcpQt.cpp 2821 2010-08-01 11:44:26Z alex $
 *  @brief
 * File containing methods for the wosh::network::qt::ServerTcpQt class.
 * The header for this class can be found in ServerTcpQt.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/
 
 #include <framework/network/qt/ServerTcpQt.h>

 #include <core/ObjectAllocator.h>
 #include <framework/network/NetworkUtilities.h>
 #include <core/Utilities.h>


#ifdef _ServerTcpQt_DEBUG_ENABLED
# define LOG( LEVEL, FORMAT, ... ) Log(LEVEL, FORMAT, __VA_ARGS__);
#else
# define LOG( LEVEL, FORMAT, ... ) ;
#endif


using namespace std;
namespace wosh {
 namespace network {
  namespace qt {

  WOSH_REGISTER(wosh::network::qt::ServerTcpQt, "wosh::network::ServerTcp", 1.0, _static_ServerTcpQt )

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////// CONSTRUCTORS

ServerTcpQt::ServerTcpQt( )
	: QTcpServer(), ServerTcp() {
#ifdef _ServerTcpQt_DEBUG_ENABLED
	Log.setIndent( 5 );
	Log.setContext( "ServerTcpQt" );
	Log.setLevel( LOG_INFO );
#endif
 }

ServerTcpQt::~ServerTcpQt() {
	LOG(LOG_VERBOSE, ":~ServerTcpQt() Destroying.." );
 }

//////////////////////////////////////////////////////////////////////////////////////////////// CONSTRUCTORS
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void ServerTcpQt::incomingConnection( int socketId ) {
	LOG(LOG_INFO, ":incomingConnection() Socket#%d", socketId );

	if ( this->listener != NULL )
		this->listener->serverTcp_incomingConnection(socketId, this);
/*
	SocketTcpQt *socket = new SocketTcpQt();
	socket->setParent(this);
	socket->setSocketDescriptor(socketId);
	QObject::connect(socket, SIGNAL(disconnected()), socket, SLOT(deleteLater()));

	if ( this->listener != NULL )
		this->listener->serverTcp_incomingConnection(this, socket);
*/
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

WRESULT ServerTcpQt::bindSocket( unsigned long ) {
	LOG(LOG_VERBOSE, ":bindSocket() Binding Server %s : %d", this->address.c_str(), this->port );

	QHostAddress tcpAddress;
	tcpAddress.setAddress( QString(this->address.c_str()) );
	bool listening = this->listen(tcpAddress, this->port);
	if (!listening) {
		LOG(LOG_CRITICAL, ":bindSocket() FAILED Binding Server %s : %d", this->address.c_str(), this->port );
		return WRET_ERR_INTERNAL;
	 }
	LOG(LOG_INFO, ":bindSocket() Binded Server %s : %d", this->address.c_str(), this->port );
	return WRET_OK;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

WRESULT ServerTcpQt::unbindSocket( unsigned long ) {
	LOG(LOG_INFO, ":unbindSocket() Unbinding Server %s : %d", this->address.c_str(), this->port );
	this->close();
	return WRET_OK;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

  }; // namespace qt
 }; // namespace network
}; // namespace wosh
